package org.kabeja.dxf.entity;

import org.kabeja.dxf.DXFConstants;
import org.kabeja.dxf.DXFVariable;
import org.kabeja.dxf.helpers.Point;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author shutao
 * @since 2022/4/24 17:47
 */
public class DXFHeader {

    private Point insBase = new Point();
    private Point extMax = new Point();
    private Point extMin = new Point();
    private Point limMax = new Point();
    private Point limMin = new Point();
    private Point ucsOrg = new Point();
    private Point ucsXDir = new Point();
    private Point ucsYDir = new Point();
    private Point pInsBase = new Point();
    private Point pExtMax = new Point();
    private Point pExtMin = new Point();
    private Point pLimMax = new Point();
    private Point pLimMin = new Point();

    private double celtScale;
    private double dimScale;
    private double filleTrad;
    private double angBase;

    private int aunits;
    private int angDir;
    private int measureMent;

    private String fingerPrintGuid;
    private String versionGuid;
    private String acadVarsion;

    private Map<String, DXFVariable> variables = new HashMap<>();

    public DXFHeader() {
    }

    public void paramConvert() {
        setPoint(DXFConstants.HEADER_VARIABLE_INSBASE, insBase);
        setPoint(DXFConstants.HEADER_VARIABLE_EXTMAX, extMax);
        setPoint(DXFConstants.HEADER_VARIABLE_EXTMIN, extMin);
        setPoint(DXFConstants.HEADER_VARIABLE_LIMMAX, limMax);
        setPoint(DXFConstants.HEADER_VARIABLE_LIMMIN, limMin);
        setPoint(DXFConstants.HEADER_VARIABLE_UCSORG, ucsOrg);
        setPoint(DXFConstants.HEADER_VARIABLE_UCSXDIR, ucsXDir);
        setPoint(DXFConstants.HEADER_VARIABLE_UCSYDIR, ucsYDir);
        setPoint(DXFConstants.HEADER_VARIABLE_PINSBASE, pInsBase);
        setPoint(DXFConstants.HEADER_VARIABLE_PEXTMAX, pExtMax);
        setPoint(DXFConstants.HEADER_VARIABLE_PEXTMIN, pExtMin);
        setPoint(DXFConstants.HEADER_VARIABLE_PLIMMAX, pLimMax);
        setPoint(DXFConstants.HEADER_VARIABLE_PLIMMIN, pLimMin);

        celtScale = getDoubleVar(DXFConstants.HEADER_VARIABLE_CELTSCALE, "40");
        dimScale = getDoubleVar(DXFConstants.HEADER_VARIABLE_DIMSCALE, "40");
        filleTrad = getDoubleVar(DXFConstants.HEADER_VARIABLE_FILLETRAD, "40");
        angBase = getDoubleVar(DXFConstants.HEADER_VARIABLE_ANGBASE, "50");

        aunits = getIntVar(DXFConstants.HEADER_VARIABLE_AUNITS, "70");
        angDir = getIntVar(DXFConstants.HEADER_VARIABLE_ANGDIR, "70");
        measureMent = getIntVar(DXFConstants.HEADER_VARIABLE_MEASUREMENT, "70");

        fingerPrintGuid = getString(DXFConstants.HEADER_VARIABLE_FINGERPRINTGUID, "2");
        versionGuid = getString(DXFConstants.HEADER_VARIABLE_VERSIONGUID, "2");
        acadVarsion = getString(DXFConstants.HEADER_VARIABLE_ACADVER, "1");
    }

    private void setPoint(String key, Point point) {
        DXFVariable variable = variables.get(key);
        if (variable != null) {
            point.setX(variable.getDoubleValue("10"));
            point.setY(variable.getDoubleValue("20"));
        }
    }

    private double getDoubleVar(String key, String code) {
        return variables.get(key).getDoubleValue(code);
    }

    private int getIntVar(String key, String code) {
        return variables.get(key).getIntegerValue(code);
    }

    private String getString(String key, String code) {
        return variables.get(key).getValue(code);
    }

    public void setVariable(DXFVariable var1) {
        this.variables.put(var1.getName(), var1);
    }

    public boolean hasVariable(String var1) {
        return this.variables.containsKey(var1);
    }

    public DXFVariable getVariable(String var1) {
        return this.variables.get(var1);
    }

    public Iterator<DXFVariable> getVarialbeIterator() {
        return this.variables.values().iterator();
    }

    public boolean isFillMode() {
        return this.hasVariable(DXFConstants.HEADER_VARIABLE_FILLMODE) &&
                this.getVariable(DXFConstants.HEADER_VARIABLE_FILLMODE).getDoubleValue("70") > 0.0D;
    }

    public double getLinetypeScale() {
        double var1 = 1.0D;
        if (this.hasVariable(DXFConstants.HEADER_VARIABLE_LTSCALE)) {
            var1 = this.getVariable(DXFConstants.HEADER_VARIABLE_LTSCALE).getDoubleValue("40");
        }

        return var1;
    }

    public Point getInsBase() {
        return insBase;
    }

    public void setInsBase(Point insBase) {
        this.insBase = insBase;
    }

    public Point getExtMax() {
        return extMax;
    }

    public void setExtMax(Point extMax) {
        this.extMax = extMax;
    }

    public Point getExtMin() {
        return extMin;
    }

    public void setExtMin(Point extMin) {
        this.extMin = extMin;
    }

    public Point getLimMax() {
        return limMax;
    }

    public void setLimMax(Point limMax) {
        this.limMax = limMax;
    }

    public Point getLimMin() {
        return limMin;
    }

    public void setLimMin(Point limMin) {
        this.limMin = limMin;
    }

    public Point getUcsOrg() {
        return ucsOrg;
    }

    public void setUcsOrg(Point ucsOrg) {
        this.ucsOrg = ucsOrg;
    }

    public Point getUcsXDir() {
        return ucsXDir;
    }

    public void setUcsXDir(Point ucsXDir) {
        this.ucsXDir = ucsXDir;
    }

    public Point getUcsYDir() {
        return ucsYDir;
    }

    public void setUcsYDir(Point ucsYDir) {
        this.ucsYDir = ucsYDir;
    }

    public Point getpInsBase() {
        return pInsBase;
    }

    public void setpInsBase(Point pInsBase) {
        this.pInsBase = pInsBase;
    }

    public Point getpExtMax() {
        return pExtMax;
    }

    public void setpExtMax(Point pExtMax) {
        this.pExtMax = pExtMax;
    }

    public Point getpExtMin() {
        return pExtMin;
    }

    public void setpExtMin(Point pExtMin) {
        this.pExtMin = pExtMin;
    }

    public Point getpLimMax() {
        return pLimMax;
    }

    public void setpLimMax(Point pLimMax) {
        this.pLimMax = pLimMax;
    }

    public Point getpLimMin() {
        return pLimMin;
    }

    public void setpLimMin(Point pLimMin) {
        this.pLimMin = pLimMin;
    }

    public double getCeltScale() {
        return celtScale;
    }

    public void setCeltScale(double celtScale) {
        this.celtScale = celtScale;
    }

    public double getDimScale() {
        return dimScale;
    }

    public void setDimScale(double dimScale) {
        this.dimScale = dimScale;
    }

    public double getFilleTrad() {
        return filleTrad;
    }

    public void setFilleTrad(double filleTrad) {
        this.filleTrad = filleTrad;
    }

    public double getAngBase() {
        return angBase;
    }

    public void setAngBase(double angBase) {
        this.angBase = angBase;
    }

    public int getAunits() {
        return aunits;
    }

    public void setAunits(int aunits) {
        this.aunits = aunits;
    }

    public int getAngDir() {
        return angDir;
    }

    public void setAngDir(int angDir) {
        this.angDir = angDir;
    }

    public int getMeasureMent() {
        return measureMent;
    }

    public void setMeasureMent(int measureMent) {
        this.measureMent = measureMent;
    }

    public String getFingerPrintGuid() {
        return fingerPrintGuid;
    }

    public void setFingerPrintGuid(String fingerPrintGuid) {
        this.fingerPrintGuid = fingerPrintGuid;
    }

    public String getVersionGuid() {
        return versionGuid;
    }

    public void setVersionGuid(String versionGuid) {
        this.versionGuid = versionGuid;
    }
}
